/*
 * create:2016年4月8日 上午9:52:47
 * author:wangds  
 */
package com.cxkj.shop.utils;

import java.util.Collection;
import java.util.Collections;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.apache.commons.lang3.StringUtils;

import com.cxkj.shop.model.order.DefaultOrder;
import com.cxkj.shop.model.order.Order;
import com.cxkj.shop.model.order.OrderItem;
import com.cxkj.shop.model.shoppingcart.ShoppingCart;
import com.cxkj.shop.model.shoppingcart.ShoppingCartUtils;
import com.cxkj.shop.model.user.DefaultPostAddress;
import com.cxkj.shop.model.user.User;
import com.cxkj.shop.proc.DefaultSelectListMybatisProc;
import com.cxkj.shop.proc.GetHttpSessionProc;
import com.cxkj.shop.proc.GetShoppingCartFromHttpSessionOrCreateProc;
import com.cxkj.shop.servlet.FillUserServlet.CONSTANTS;

import net.wangds.comp.business.http.HttpBusiness;

/**
 * .
 * 
 * @author wangds
 *
 */
public final class BusinessUtils extends AbstractBusinessUtils {

  public static HttpSession getHttpSessionFromHttpBusiness(HttpBusiness biz, boolean forceCreate) {
    HttpSession res = null;
    GetHttpSessionProc proc = new GetHttpSessionProc();
    proc.setBusinessObject(biz);
    proc.setForceCreate(forceCreate);
    proc.process();
    res = proc.getResult();
    return res;
  }

  public static HttpSession getHttpSessionFromHttpBusiness(HttpBusiness biz) {
    return getHttpSessionFromHttpBusiness(biz, true);
  }

  public static ShoppingCart getShoppingCartFromSession(HttpSession session) {
    GetShoppingCartFromHttpSessionOrCreateProc proc = new GetShoppingCartFromHttpSessionOrCreateProc();
    proc.setSession(session);
    proc.process();
    ShoppingCart res = proc.getResult();
    
    ShoppingCartUtils.checkFreebie(res);
    
    String openid = BusinessUtils.getOpenIdSession(session);
    User user = UserUtils.selectUserByOpenId(openid);
    ShoppingCartUtils.updateFreeCards(res, user);
    
    
    return res;
  }

  @SuppressWarnings("unchecked")
  public static Map<String, Object> getUserInfoFromSession(HttpSession session) {
    return (Map<String, Object>) session.getAttribute(CONSTANTS.KEY_SESSION_USERINFO_MAP);
  }

  @SuppressWarnings("unchecked")
  public static Map<String, Object> getTokenMapFromSession(HttpSession session) {
    return (Map<String, Object>) session.getAttribute(CONSTANTS.KEY_SESSION_TOKEN_MAP);
  }

  @SuppressWarnings("unchecked")
  public static String getOpenIdSession(HttpSession session) {
    String res = "";
    Map<String, Object> map = (Map<String, Object>) session.getAttribute(CONSTANTS.KEY_SESSION_TOKEN_MAP);
    if (map != null) {
      res = (String) map.get(CONSTANTS.KEY_WEIXIN_OPEN_ID);
    }
    
    if(StringUtils.isBlank(res)){
      res="oP_f5viGo2PKyekcbA5UszaTXZ-Q";
    }
    
    return res;
  }

  public static Collection<DefaultPostAddress> loadAddressByOpenId(String openId) {
    Collection<DefaultPostAddress> res = Collections.emptyList();
    DefaultSelectListMybatisProc<String, DefaultPostAddress> proc = getContext().getBean("loadAddressByOpenId");
    proc.setParameter(openId);
    proc.process();
    res = proc.getResult();
    return res;
  }

  public static boolean saveAddressIfIsNew(DefaultPostAddress add) {
    // TODO: TSK2016041302 保存新地址
    // TODO: 需要判断新地址
    return true;
  }
  //zhao_f
  public static boolean setUpdatePayTimeOrder(Order order){
	  boolean res=false;
	  res=execUpdateSql("updatePayTimerOrder",order);
	  return res;
  }
  public static boolean setUpdatePayTimerToFinishTimeOrder(Order order){
	  boolean res=false;
	  res=execUpdateSql("updatePayTimerToFinishTimerOrder",order);
	  return res;
  }
  public static boolean setUpdateFinishTimeOrder(Order order){
	  boolean res=false;
	  res = execUpdateSql("updateFinishTimeOrder",order);
	  return res;
  }
  //zhao_f
  public static Order createNewOrder(String openId, Collection<OrderItem> items) {
    Order res = new DefaultOrder();
    res.setOpenId(openId);
    res.setCreateTm(System.currentTimeMillis());
    res.getItems().addAll(items);
    // TODO: TSK2016041303 保存新生成的订单
    return res;
  }

  public static boolean savePayInfo(Order order, String payId) {
    boolean res = false;
    // TODO: TSK2016041305 保存订单支付信息
    return res;
  }


}
